package SA.teacher.daily;

import java.util.ArrayList;
import java.util.Calendar;

import SA.teacher.MainActivity;
import SA.teacher.R;
import SA.teacher.manage.ManageActivity1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class DailyActivity1 extends Activity {

	ArrayList<TextView> list;
	TextView calendarTitle;
	int firstDay;
	int totDays;
	int iYear;
	int iMonth;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daily1);

		Calendar calendar = Calendar.getInstance();
		iYear = calendar.get(Calendar.YEAR);
		iMonth = calendar.get(Calendar.MONTH);
		calendarTitle = (TextView) findViewById(R.id.today);
		list = new ArrayList<TextView>();

		TableLayout table = (TableLayout) findViewById(R.id.calendar);

		int i, j;
		for (i = 0; i < 6; i++) {
			TableRow tr = new TableRow(this);
			for (j = 0; j < 7; j++) {
				TextView tv = new TextView(this);
				if (j == 0)
					tv.setTextColor(Color.RED);
				else if (j == 6)
					tv.setTextColor(Color.BLUE);
				else
					tv.setTextColor(Color.BLACK);
				tv.setTextSize(25);
				tv.setGravity(Gravity.CENTER_HORIZONTAL);
				tr.addView(tv);
				list.add(tv);
			}
			table.addView(tr);
		}
		table.setStretchAllColumns(true);
		setCalendar(iYear, iMonth);

		ImageView preBtn = (ImageView) findViewById(R.id.pre);
		preBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				iMonth--;
				if (iMonth <= 0) {
					iYear--;
					iMonth = 11;
				}
				setCalendar(iYear, iMonth);
			}
		});

		ImageView nextBtn = (ImageView) findViewById(R.id.next);
		nextBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				iMonth++;
				if (iMonth >= 12) {
					iMonth = 0;
					iYear++;
				}
				setCalendar(iYear, iMonth);
			}
		});
	}

	private void setCalendar(final int year, final int month) {
		Calendar todayCalendar = Calendar.getInstance();
		int todayYear;
		int todayMonth;
		int todayDate;

		todayYear = todayCalendar.get(Calendar.YEAR);
		todayMonth = todayCalendar.get(Calendar.MONTH);
		todayDate = todayCalendar.get(Calendar.DATE);

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		calendarTitle.setText(year + "년" + (month + 1) + "월");

		int whatDay = calendar.get(Calendar.DAY_OF_WEEK);
		int i;
		int j = 1;
		final String[] classes = new String[] { "1 - A", "1 - B", "2 - A" };
		for (i = whatDay - 1; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + whatDay - 1; i++) {
			if ((year == todayYear) && (month == todayMonth)&& (j == todayDate)) {
				list.get(i).setBackgroundColor(Color.rgb(185, 225, 255));
			}else{
				list.get(i).setBackgroundColor(Color.WHITE);
			}
			list.get(i).setText(j++ + "");

			final int today = j;
			list.get(i).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					AlertDialog.Builder bld = new AlertDialog.Builder(DailyActivity1.this);
					bld.setTitle("반을 선택해주세요.")

					.setItems(classes,new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog,	int which) {
							Intent intent = new Intent(DailyActivity1.this, DailyToday2.class);
							intent.putExtra("classname",classes[which]);
							intent.putExtra("year",Integer.toString(year));
							intent.putExtra("month",Integer.toString(month + 1));
							intent.putExtra("date",Integer.toString(today - 1));
							
							startActivity(intent);
						}
					}).show();
				}
			});
		}
	}
}